﻿using Domain;
using ErrorCode;
using Infrastructure.Data;
using MediatR;
using SqlSugar;
using WriteAPI.Command.Material;
using WriteAPI.Command.生产计划;

namespace WriteAPI.CommandHandle.生产计划
{
    public class GetProductionPlanCommandHandler : IRequestHandler<GetProductionPlanCommand, APIPaging<Lwp_ProductionPlan>>
    {


        private readonly ISqlSugarClient db;


        public GetProductionPlanCommandHandler(ISqlSugarClient db)
        {
            this.db = db;

        }

        public async Task<APIPaging<Lwp_ProductionPlan>> Handle(GetProductionPlanCommand request, CancellationToken cancellationToken)
        {

            APIPaging<Lwp_ProductionPlan> result = new APIPaging<Lwp_ProductionPlan>();
            List<Lwp_ProductionPlan> list = db.Queryable<Lwp_ProductionPlan>().ToList();
            result.TotalCount = list.Count();

            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.size);

            result.Items = list.OrderByDescending(x => x.Id).Skip((request.index - 1) * request.size).Take(request.size).ToList();
            return await Task.FromResult(result);
        }
    }
}
